import des from "../../utils/des";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
        id: '1',
        name: '金融机构'
      },
      {
        id: '2',
        name: '政府机构'
      },
    ],
    fileList: [],
    islodding: false,
    id: '',
    form: {
      form_name: '',
      form_bank: '',
      form_bank1: '',
      form_gong: '',
      form_zfType1: '1',
      form_zfType2: '',
      form_role1: '1',
      form_role2: '',
      form_is_participate: '0',
    },
    formZf: {
      form_name: '',
      branch_name: ''
    },
    type: '',
    viewType: '1',
  },
  isZoufang(e) {
    this.setData({
      ["form.form_is_participate"]: e.detail,
    });
  },
  onChangeTab(e) {
    this.setData({
      viewType: e.currentTarget.dataset.id
    })
  },
  async goSubmit() {
    console.log(this.data.type)
    if (!this.data.form.form_name) {
      wx.showToast({
        title: '请填写用户姓名',
        icon: 'none'
      })
      return
    }else {
      let text=/^[\u4e00-\u9fa5]{2,4}$/
      if(!text.test(this.data.form.form_name)){
        wx.showToast({
          title: '请填写真实姓名',
          icon: 'none'
        })
        return
      }
      
    }
    
    if (!this.data.form.form_bank) {
      wx.showToast({
        title: '请选择所在银行',
        icon: 'none'
      })
      return
    }
    if (!this.data.form.form_bank1) {
      wx.showToast({
        title: '请选择所在分支行',
        icon: 'none'
      })
      return
    }
    if (!this.data.form.form_gong) {
      wx.showToast({
        title: '请填写银行系统工号',
        icon: 'none'
      })
      return
    }
    // 处理用户类型
    // sec_admin 二级 F48FABCC550048C9E053541012AC6AFB
    // cbrc 银监
    // fir_admin 一级 F48FABCC550248C9E053541012AC6AFB
    // manager 客户经理 F48FABCC550348C9E053541012AC6AFB
    let roles = ''
    if (this.data.form.form_role1 == '1' && this.data.form.form_role2 == '1') {
      roles = "F48FABCC550048C9E053541012AC6AFB,F48FABCC550348C9E053541012AC6AFB"
    } else if (this.data.form.form_role1 == '1' && this.data.form.form_role2 != '1') {
      roles = "F48FABCC550348C9E053541012AC6AFB"
    } else {
      roles = "F48FABCC550048C9E053541012AC6AFB"
    }
    // 处理走访类型
    let visitType = ''
    if (this.data.form.form_zfType1 == '1' && this.data.form.form_zfType2 == '1') {
      visitType = '3'
    } else if (this.data.form.form_zfType1 == '1' && this.data.form.form_zfType2 != '1') {
      visitType = '1'
    } else {
      visitType = '2'
    }
    const params = {
      name: this.data.form.form_name,
      branchBankId: this.data.id,
      branchBank: this.data.name,
      bankId: this.data.branchId,
      bankName: this.data.branchName,
      jobNumber: this.data.form.form_gong,
      jobCardUrl: this.data.form.form_pic,
      userId: wx.getStorageSync("userInfo").userId,
      visitType: visitType,
      roles: roles,
      isParticipate: this.data.form.form_is_participate
    }
    console.log(params, '金融提交的参数')
    const res = await getApp().globalData.request(
      "/user/bind_apply", params,
      "POST"
    );
    console.log(res, '返回的参数')
    if (res.code == 200) {
      wx.redirectTo({
        url: `../../packageA/pages/successBind/successBind?type=${this.data.type}`,
      })
    } else {
      wx.showToast({
        title: '已提交绑定信息，请勿重复申请',
        icon: 'none'
      })
    }
  },
  async goSubmitZf() {
    if (!this.data.formZf.form_name) {
      wx.showToast({
        title: '请填写用户姓名',
        icon: 'none'
      })
      return
    }
    if (!this.data.formZf.branch_name) {
      wx.showToast({
        title: '请填写所在部门',
        icon: 'none'
      })
      return
    }
    const params = {
      name: this.data.formZf.form_name,
      branchBank: this.data.formZf.branch_name,
      branchBankId: 'bindGOV'
    }
    console.log(params, '政府提交的参数')
    const res = await getApp().globalData.request(
      "/user/bind_apply", params,
      "POST"
    );
    if (res.code == 200) {
      wx.redirectTo({
        url: `../../packageA/pages/successBind/successBind?type=${this.data.type}`,
      })
    } else {
      wx.showToast({
        title: '已提交绑定信息，请勿重复申请',
        icon: 'none'
      })
    }
  },
  onChangeMore1(e) {
    // console.log(e, 'e')
    const value = e.currentTarget.dataset.value
    const key = e.currentTarget.dataset.key
    if (this.data.form[key] && this.data.form.form_role1 == '1' && this.data.form.form_role2 == '1') {
      this.data.form[key] = ''
    } else {
      this.data.form[key] = value
    }
    this.setData({
      form: this.data.form
    })
  },
  onChangeMore2(e) {
    // console.log(e, 'e')
    const value = e.currentTarget.dataset.value
    const key = e.currentTarget.dataset.key
    if (this.data.form[key] && this.data.form.form_zfType1 == '1' && this.data.form.form_zfType2 == '1') {
      this.data.form[key] = ''
    } else {
      this.data.form[key] = value
    }
    this.setData({
      form: this.data.form
    })
  },

  editName(e) {
    this.setData({
      ['form.form_name']: e.detail.value
    })
  },
  editNameZf(e) {
    const key = e.currentTarget.dataset.key
    this.setData({
      [`formZf.${key}`]: e.detail.value
    })
  },
  editGong(e) {
    this.setData({
      ['form.form_gong']: e.detail.value
    })
  },
  onChangeType(e) {
    console.log(e);
    this.setData({
      ['form.form_zfType']: e.detail
    })
  },
  selectImg() {
    var that = this
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      sizeType: ['original', 'compressed'],
      success(res) {
        console.log(res);
        const tempFilePaths = res.tempFiles
        that.data.fileList.push({
          ...tempFilePaths[0],
          url: tempFilePaths[0].tempFilePath,
          deletable: true,
        })
        that.setData({
          fileList: that.data.fileList,
          islodding: true
        })
        // 上传文件 
        wx.showLoading({
          title: '上传中',
        })
        wx.uploadFile({
          // url: 'http://192.168.1.114:20100/api/fileApi/cosUpload',
          url: 'https://fintech-wx.szjrgzjg.com/api/fileApi/cosUpload',
          filePath: tempFilePaths[0].tempFilePath,
          name: 'file',
          formData: {
            file: tempFilePaths[0],
            folder: 'mini_accountBind'
          },
          header: {
            token: wx.getStorageSync("token")
          },
          success(res) {
            var data = des.decrypt(
              JSON.parse(res.data).data,
              "98fbffd1064a4355b8abaacb6fa96f94"
            );
            console.log(JSON.parse(data));
            wx.hideLoading()
            that.setData({
              ["form.form_pic"]: JSON.parse(data).data,
              islodding: false
            })
          },
          fail(err) {
            console.log(err);
          }
        })
      }
    })
  },
  deleteImg() {
    this.data.fileList.pop()
    this.setData({
      fileList: this.data.fileList,
    })
    getApp().globalData.request("/api/fileApi/cosDelete", {
      url: this.data.form.zf_img
    }, "POST").then(res => {
      console.log(res);
      if (res.code == 200) {
        this.setData({
          ["form.zf_img"]: ""
        })
        wx.showToast({
          title: res.data,
        })
      } else {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  selectBank1() {
    wx.navigateTo({
      url: '/pages/bankList/bankList',
    })
  },
  selectBank2() {
    if (!this.data.form.form_bank) {
      wx.showToast({
        title: '请先选择所在银行',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: `/pages/branchkList/branchkList?parentId=${this.data.id}`,
    })
  },
  // goShenhe () {
  //   wx.navigateTo({
  //     url: "/pages/accountAuth/accountAuth_success/accountAuth_success",
  //   });
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const type = options.type
    this.setData({
      type: type,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data.name, 'this.data.name')
    if (this.data.name !== this.data.form.form_bank) {
      this.setData({
        ["form.form_bank"]: this.data.name,
        ["form.form_bank1"]: '',
        branchName: ''
      })
    } else {
      this.setData({
        ["form.form_bank"]: this.data.name,
        ["form.form_bank1"]: this.data.branchName ? this.data.branchName : this.data.form.form_bank1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})